package com.peng.leetcode.other;

/**
 * LengthOfLastWord
 *
 * @author: lupeng6
 * @create: 2020/12/9 15:33
 */
public class LengthOfLastWord {


    public static void main(String[] args) {
        String s = "a ";
        System.out.println(new Solution().lengthOfLastWord(s));
    }

    static class Solution {
        public int lengthOfLastWord(String s) {
            s = s.trim();
            if (s.length() == 0) {
                return 0;
            }

            int start = 0;
            int end = 0;
            boolean flag = false;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c != ' ') {
                    end = i;
                    if (flag) {
                        start = i;
                        flag = false;
                    }
                } else {
                    flag = true;
                }
            }

            return end - start + 1;
        }
    }

}
